package com.ndp.fb.util;

import com.alibaba.fastjson.JSON;
import com.ndp.fb.config.ConfigCentre;
import org.apache.commons.codec.digest.DigestUtils;

import java.util.HashMap;


public class MessageHelper {

    /**
     * 邮件发送服务地址
     */
//    private static String mailApiServer = ConfigCentre.getString("email.api.server");
//    private String app = ConfigCentre.getString("email.api.app");
//    private String key = ConfigCentre.getString("email.api.key");
//    private String from = ConfigCentre.getString("email.api.from");

    /**
     * 发送邮件
     */
    public static boolean sendMail(String to, String subject, String body) {
        HashMap<String, String> paramMap = new HashMap<>();
        paramMap.put("app", "fmp_mail");
        paramMap.put("key", "e1#2x7m");
        String time = System.currentTimeMillis()/1000+"";
        paramMap.put("time", time);
        String md5 =  DigestUtils.md5Hex(("fmp_maile1#2x7m"+time).getBytes());
        paramMap.put("sign", md5.substring(0, 8));
        paramMap.put("from", "fmp@ndpmedia.com");
        paramMap.put("to", to);
        paramMap.put("subject", subject);
        paramMap.put("body", body);
        String result = HttpClientUtils.httpPost(ConfigCentre.getString("email.api.server"), paramMap);
        int code = JSON.parseObject(result).getIntValue("code");
        if (code == 0) {
            return true;
        }
        return false;
    }
    /**
     * 发送短信
     * @param to
     * @param subject
     * @param body
     * @return
     */
    public static boolean sendMessage(String to, String ip, String body) {
        HashMap<String, String> paramMap = new HashMap<>();
        paramMap.put("app", "fmp_sms");
        paramMap.put("key", "6s!#3a89H");
        String time = System.currentTimeMillis()/1000+"";
        paramMap.put("time", time);
        String md5 =  DigestUtils.md5Hex(("fmp_sms6s!#3a89H"+time).getBytes());
        paramMap.put("sign", md5.substring(0, 8));
        paramMap.put("ip", ip);
        paramMap.put("to", to);
        paramMap.put("text", body);
        paramMap.put("model", "sms");
        String result = HttpClientUtils.httpPost(ConfigCentre.getString("email.api.server"), paramMap);
        int code = JSON.parseObject(result).getIntValue("code");
        if (code == 0) {
            return true;
        }
        return false;
    }
    public static void main(String[] args) {
    	HashMap<String, String> paramMap = new HashMap<>();
        paramMap.put("app", "fmp_sms");
        paramMap.put("key", "6s!#3a89H");
        String time = System.currentTimeMillis()/1000+"";
        paramMap.put("time", time);
        String md5 =  DigestUtils.md5Hex(("fmp_sms6s!#3a89H"+time).getBytes());
        paramMap.put("sign", md5.substring(0, 8));
        paramMap.put("ip", "12.112.11.10");
        paramMap.put("to", "18611623083");
        paramMap.put("text", "这是一条测试短信dasda");
        paramMap.put("model", "sms");
        String result = HttpClientUtils.httpPost("http://notify.ymtech.info/notify/api/send", paramMap);
        int code = JSON.parseObject(result).getIntValue("code");
        System.out.println(code);
	}
}